package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.EmployeeDao;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.IEmployeeSevice;
import com.itheima.reggie.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao,Employee>
    implements IEmployeeSevice {



    @Autowired
    EmployeeDao employeeDao;

    @Override
    public Map checkLogin(Employee emp) {
        Map resultMap = new HashMap();
        //1. 查询数据表employee,查询指定的用户名称是否存在
        //select * from employee where username=?

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,emp.getUsername());
        Employee employee = employeeDao.selectOne(queryWrapper);

        if(employee==null){
            resultMap.put("msg","用户名不存在");
            return resultMap;
        }

        //2. 如果查询到，用户信息存在
        //校验用户密码是否正确：Md5加密 / Bcrypt(加盐加密)
        //用户输入的明文：12345   和数据表中查询到的密文：e10adc3949ba59abbe56e057f20f883e
        String userInputPwd = MD5Utils.code(emp.getPassword());
        if(!userInputPwd.equals(employee.getPassword())){
            resultMap.put("msg","密码输入有误");
            return resultMap;
        }

        //3. 判断用户的状态是否启动（status=0 账号禁用）
        if(employee.getStatus()==0){
            resultMap.put("msg","账户被禁用");
            return resultMap;
        }

        //4. 登录成功
        resultMap.put("emp",employee);

        return resultMap;
    }
}
